var express = require('express');
var router = express.Router();
const conf = require('../config')
const cache = require('cache-all/memory');
let stocks = require('../Model/stock.js');
let player = require('../Model/player.js');
let server = require('../Model/server.js');
let optionUnit = require('../Model/optionUnit.js');
let order = require('../Model/order.js');
let UUID = require('uuid')
router.get('/getOrder', async function (req, res, next) {
  let newOrder=await order.findOneAndRemove()
  if(newOrder){
    res.send("export async function main(ns) {localStorage.setItem('"+newOrder.t+"',JSON.stringify("+newOrder.d+"))}")
  }else{
    res.send("export async function main(ns) {console.warn('no new order')}")
  }
});


router.post('/order', async function (req, res, next) {
  let type=req.body.type
  let data=req.body.data
  let newOrder=new order({
    _id:UUID.v1(),
    t:type,
    d:data
  })
  await newOrder.save()
  res.json({ hasError:false,msg:'操作成功'});
});

router.get('/server', async function (req, res, next) {
  let a = JSON.parse(req.query.q)
  
  await server.findOneAndUpdate({
    s:a.hostname
  },{
    s:a.hostname,
    t:new Date(),
    d:a
  },{upsert:true})
  res.json({ msg: true });
});
router.get('/player', async function (req, res, next) {
  let a = JSON.parse(req.query.q)
  await player.findOneAndUpdate({
    
  },{
    t:new Date(),
    d:a
  },{upsert:true})
  res.json({ msg: true });
});
router.get('/optionUnit', async function (req, res, next) {
  let a = JSON.parse(req.query.q)
  await optionUnit.findOneAndUpdate({
    
  },{
    t:new Date(),
    d:a
  },{upsert:true})
  res.json({ msg: true });
});
router.get('/stock', async function (req, res, next) {
  let a = req.query.q
  let newP = new stocks({
    _id: UUID.v1(),
    t: new Date(),
    p: JSON.parse(a)
  })
  await newP.save()
  // console.log(json)

  res.json({ msg: true });
});

router.get('/stock', async function (req, res, next) {
  let a = req.query.q
  let newP = new stocks({
    _id: UUID.v1(),
    t: new Date(),
    p: JSON.parse(a)
  })
  await newP.save()
  // console.log(json)

  res.json({ msg: true });
});
router.get('/cmd', async function (req, res, next) {
  let a = req.query.q
  let cmd=await cache.get(a)
  // console.log(json)
  res.send("export async function main(ns) {localStorage.setItem('WDS','"+cmd.toString()+"')}")
});
router.post('/optionUnit', async function (req, res, next) {
  let data = await optionUnit.findOne()
  res.json({ data: data });
});
router.post('/player', async function (req, res, next) {
  let data = await player.findOne()
  res.json({ data: data });
});
router.post('/server', async function (req, res, next) {
  let data = await server.find()
  let opt = await optionUnit.findOne()
  res.json({ data: data,optionUnit:opt.d });
});
router.post('/stock', async function (req, res, next) {
  let n=parseInt(req.body.n)
  n=(n>0)?n:1000
  let data = await stocks.find().limit(n).sort({ t: -1 })
  // console.log(json)

  res.json({ data: data });
});
router.post('/cmd', async function (req, res, next) {
  let data = req.body.data
  for (let ii = 0; ii < data.length; ii++) {
    cache.set(data[ii].name, (data[ii].growth > 0) ? true : false)
  }
  res.json({a:1});

});
module.exports = function (app) {
  io = app.io;
  return router;
};
